package nborm;

import android.animation.TypeConverter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by YuanChao on 18-12-24.
 */
public class TableInfo {

    private static final String TAG = "TableInfo";

    /**
     * 类型转换器map.key:字段类型，value，类型转换器。
     */
    private Map<String, TypeConverter> typeConverterMap;

    /**
     * 表名。
     */
    private String mTableName;

    /**
     * 主键
     */
    private String mTableId;

    /**
     * 是否是自增长。
     */
    private boolean mAutoincrement;

    /**
     * 字段 key:字段名：value：字段类型
     */
    private Map<String, Field> mColumns;

    /**
     * 生成的建表语句。
     */
    private String mCreateTableSql;

    /**
     * 字名称列表。
     */
    private List<String> mColumnList;

    /**
     * 唯一的字段。
     */
    private Field mUniqueField;

    public String getTableId() {
        return mTableId;
    }

    public void setTableId(String tableId) {
        mTableId = tableId;
    }

    public Map<String, Field> getColumns() {
        return mColumns;
    }

    public void setColumns(Map<String, Field> columns) {
        mColumns = columns;
    }


    public String getTableName() {
        return mTableName;
    }

    public void setTableName(String tableName) {
        mTableName = tableName;
    }

    public String getCreateTableSql() {
        return mCreateTableSql;
    }

    public void setCreateTableSql(String createTableSql) {
        mCreateTableSql = createTableSql;
    }

    public boolean isAutoincrement() {
        return mAutoincrement;
    }

    public void setAutoincrement(boolean autoincrement) {
        mAutoincrement = autoincrement;
    }

    /**
     * 获取字段集合。
     *
     * @return
     */
    public List<String> getColumnList() {
        if (getColumns() == null)
            return null;
        if (mColumnList == null) {
            mColumnList = new ArrayList<>(getColumns().keySet());
            if (isAutoincrement()) {
                mColumnList.remove(mTableId);
            }
        }
        return mColumnList;
    }

    /**
     * 获取主建值。
     *
     * @param object
     * @return
     */
    public Object getTableIdValue(Object object) {
        Field field = getColumns().get(getTableId());
        try {
            return field.get(object);
        } catch (IllegalAccessException e) {
            Log.e(TAG, String.valueOf(e));
        }
        return "";
    }

    public Object getColumnValue(String columnName, Object object) {
        Field field = getColumns().get(columnName);
        try {
            return field.get(object);
        } catch (IllegalAccessException e) {
            Log.e(TAG, String.valueOf(e));
        }
        return null;

    }

    public void setUniqueField(Field uniqueField) {
        mUniqueField = uniqueField;
    }

    /**
     * 表是否存在
     *
     * @return
     */
    public boolean isTableExists(SQLiteDatabase database) {
        try {
            Cursor cursor = database.rawQuery("select * " +
                    "from " + getTableName() + " limit 0", null);
            cursor.close();
            return true;
        } catch (Exception e) {
            Log.d(TAG, e.getMessage());
            return false;
        }
    }

    public void dropTable() {
        SQLiteDatabase sqLiteDatabase = DBHelper.get().getDb();
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + getTableName());
        sqLiteDatabase.close();
    }

    public Map<String, TypeConverter> getTypeConverterMap() {
        if (typeConverterMap == null) {
            typeConverterMap = new ConcurrentHashMap<>();
        }
        return typeConverterMap;
    }

}
